#include <stdio.h>
#include <Windows.h>
#include <string.h>
#include <conio.h>

#include "frame.h"
#include "graph.h"
#include "strlib.h"



int KeyBoardInput(){
    int ret = 0;
   
    int op = getch();  
    
    switch ( op ){
    /* up */
    case 72:case 'w':case 'W':    
        ret = KB_UP;
        break;

    /*向下*/
    case 80:case 's':case 'S':
        ret = KB_DOWN;
        break;

    case 77:case 'd':case 'D':
        ret = KB_RIGHT;
        break;

    case 75:case 'a':case 'A':
        ret = KB_LEFT;
        break;

    //回车
    case 13:
        ret = KB_RETURN;
        break;

    case 27:
        ret = KB_ESC;
        break;

    default:
        ret = KB_DEFAULT;

        break;
    }//end switch
    
    return ret;
}




static int BgColor = 0;
static int FontColor = 0;

static int Hcolor(){
    return BgColor * 16 + FontColor;
}

static void setcolor(){
    HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute(hOut, Hcolor());
}

/*
 * Function: MovePen
 * Usage: MovePen(x, y);
 * ---------------------
 * This procedure moves the current point to the position
 * (x, y), without drawing a line.  The model is that of
 * the pen being lifted off the graphics window surface and
 * then moved to its new position.
 */
void MovePen(int x, int y){
    if ( x<0 )
        x = 0;
    if ( y<0 )
        y = 0;
    COORD pos = {x, y};
    HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleCursorPosition(hOut, pos);
}



/* 画一个矩形 */
void drawRectangle(int x, int y, int w, int h){
    char label[5];
    sprintf(label, "%%%ds", w);
    
    for (int i=0; i<h; i++){
        MovePen(x, y+i);
        printf(label, "");
    }

}



/* 格式化输出文本*/
void drawLabel(int x, int y, int w, int align, const char *text, int fillflag){
    if ( fillflag ){
        char label[5];
        sprintf(label, "%%%ds", w);
        
        MovePen(x, y);
        printf(label, "");
    }

    int labelX = x, labelY = y;

    switch ( align ){
    case 0:
        labelX += (w - strlen(text)) / 2;
        break;
    case 1:
        labelX += w - strlen(text);
        break;
    case -1:
        break;    
    default:
        break;
    }

    MovePen(labelX, labelY);
    printf("%s", text);
}




/*
 * 作用；设置/获取字体的颜色
 * 用法：setFontColor(Red);
 *       color = getFontColor();
 */
void setFontColor(int color){
    FontColor = color;
    setcolor();
}


int getFontColor(){
    return FontColor;
}



/*
 * 作用；设置/获取字体的颜色
 * 用法：setBgColor(Red);
 *       color = getBgColor();
 */
void setBgColor(int color){
    BgColor = color;
    setcolor();
}


int getBgColor(){
    return BgColor;
}


void nextLine(){
    printf("\n");
}


//定义隐藏光标函数
void HideCursor(){
    CONSOLE_CURSOR_INFO cursor;
    cursor.bVisible = FALSE;
	cursor.dwSize = sizeof(cursor);
	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleCursorInfo(handle, &cursor);
}

//定义隐藏光标函数
void ShowCursorA(){
    CONSOLE_CURSOR_INFO cursor;
    cursor.bVisible = TRUE;
	cursor.dwSize = sizeof(cursor);
	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleCursorInfo(handle, &cursor);
}


// 自定义gets()
int s_gets(char buf[], int buflen){
    int ret = 1, top = 0;
    buf[0] = '\0';
    
    int op = getch();
    while ( op!=13 && op!=27) {
        switch ( op ){
        case /* backspace */ 8:
            if ( top ){
                buf[--top] = '\0';
                printf("\b \b");
            }
            break;
        case 72: case 80: case 75: case 77:
        /* up down left right*/
            break;
        default:
            if ( op<='9' && op>'0' )
                return 20 + (int)(op-'0');
            if ( top < buflen-1 ){
                buf[top] = (char)op;
                buf[++top] = '\0';
                printf("%c", (char)op);
            }
            break;
        }
        op = getch();
    }
    if ( op==27 )
        ret = 0;
    return ret ;
}


void reFresh( ) 
{
    system("cls");
}

void setAtt( char *mode ) {
    str att;
    char buf[80];

    // 标题
    if ( SUCCESS == (att=getAtt(mode, "title")).state ) {
        sprintf(buf, "title %s", att.text);
        system(buf);
    }
    // 色调
    if ( SUCCESS == (att=getAtt(mode, "color")).state ) {
        sprintf(buf, "color %s", att.text);
        system(buf);
    }



}